//
//  CaretakerTabViewController.m
//  Seizure
//
//
// Holds all the tabs of the Caretaker


#import "CaretakerTabViewController.h"
#import "PatientControlsController.h"
#import "CaretakerSettingsController.h"

#import "DataManager.h"

@class LinkedAccount;

@implementation CaretakerTabViewController

@synthesize settings;
@synthesize patientControls;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        PatientControlsController *pcControl = [[PatientControlsController alloc] initWithNibName:@"PatientControlsController" bundle:nil];
        CaretakerSettingsController *csControl = [[CaretakerSettingsController alloc] initWithNibName:@"CaretakerSettingsController" bundle:nil];
        
        NSArray *controllers = [[NSArray alloc] initWithObjects: csControl, pcControl, nil];
        [self setViewControllers:controllers animated:YES];
        
        LinkedAccount *linked = [[DataManager sharedInstance] fetchPatient];

        if( linked.email.length == 0 && linked.name.length == 0 ){
            self.selectedViewController = csControl;
        }
        else{
            self.selectedViewController = pcControl;
        }
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Tab Bar Delegate Method

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item == patientControls) {
        self.selectedViewController = [self.viewControllers objectAtIndex:0];
    } 
    else if (item == settings) {
        self.selectedViewController = [self.viewControllers objectAtIndex:1];
    }
}

@end
